﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ForzaGT3.Data;
using ForzaGT3.Areas.Admin.Models;
using ForzaGT3.Common.Configuration;

namespace ForzaGT3.Areas.Admin.Controllers
{
    public partial class NewsController : Controller
    {
        //
        // GET: /Admin/News/

        public virtual ActionResult Index()
        {
            // Get paging values from the query string
            int pageNum = CustomGrid.QueryParams.getPageNumber("data", Request.QueryString);
            int pageSize = CustomGrid.QueryParams.getPageSize("data", Request.QueryString);

            // Get sorting values from query string
            string sort = CustomGrid.QueryParams.getSortField("data", Request.QueryString, new Series(), "CreatedDate");
            string stype = CustomGrid.QueryParams.getSortType("data", Request.QueryString, "desc");

            // Get filter where conditions and values from the query string            
            List<object> whereValues = new List<object>();
            string whereConditions = "";
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "NewsUrl", Request.QueryString, whereValues);
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "NewsTitle", Request.QueryString, whereValues);

            

            // Get data from database
            int totalRecords;
            IQueryable<News> data = News.AdminGetAll(whereConditions, whereValues, sort, stype, pageNum, pageSize, out totalRecords);

            // Count the total number of records
            ViewBag.totalRecords = totalRecords;
            ViewBag.CurrentPageName = "News";

            return View(data.ToList());
        }

        public virtual ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public virtual ActionResult Add(NewsModel model)
        {
            if (ModelState.IsValid)
            {
                News news = new News();
                news.NewsId = Guid.NewGuid();
                if (model.Image != null)
                {
                    model.Image.SaveAs(Server.MapPath("~" + SiteSettings.NewsImageFolder) + model.Image.FileName);
                    news.ImageUrl = SiteSettings.NewsImageFolder + model.Image.FileName;
                }
                news.NewsTitle = model.Title;
                news.NewsIntro = model.Intro;
                news.NewsContent = model.Content;
                news.NewsUrl = model.Url;
                news.Keywords = model.Keywords;

                news.NewsUrlEng = model.UrlEnglish;
                news.NewsIntroEng = model.IntroEnglish;
                news.NewsContentEng = model.ContentEnglish;
                news.NewsTitleEng = model.TitleEnglish;
                news.KeywordsEng = model.KeywordsEnglish;

                news.IsPublished = model.IsPublished;
                news.IsDeleted = false;
                news.CreatedDate = DateTime.Now;
                news.CreatedBy = HttpContext.User.Identity.Name;
                news.Insert();
                return RedirectToAction(MVC.Admin.News.Index());
            }
            return View(model);
        }

        public virtual ActionResult Edit(Guid Id)
        {
            News news = News.GetById(Id);
            NewsModel model = new NewsModel();
            if (news != null)
            {
                model.Url = news.NewsUrl;
                model.Title = news.NewsTitle;
                model.Intro = news.NewsIntro;
                model.Content = news.NewsContent;
                model.Keywords = news.Keywords;

                model.UrlEnglish = news.NewsUrlEng;
                model.TitleEnglish = news.NewsTitleEng;
                model.IntroEnglish = news.NewsIntroEng;
                model.ContentEnglish = news.NewsContentEng;
                model.KeywordsEnglish = news.KeywordsEng;
                
                model.IsPublished = news.IsPublished;
                model.Id = news.NewsId;
                model.ImageUrl = news.ImageUrl;
            }
            return View(model);
        }

        [HttpPost]
        public virtual ActionResult Edit(NewsModel model)
        {
            if (ModelState.IsValid)
            {
                News news = News.GetById(model.Id);
                if (news != null)
                {
                    news.NewsTitle = model.Title;
                    news.NewsIntro = model.Intro;
                    news.NewsContent = model.Content;
                    news.NewsUrl = model.Url;
                    news.Keywords = model.Keywords;
                    news.IsPublished = model.IsPublished;

                    news.NewsUrlEng = model.UrlEnglish;
                    news.NewsIntroEng = model.IntroEnglish;
                    news.NewsContentEng = model.ContentEnglish;
                    news.NewsTitleEng = model.TitleEnglish;
                    news.KeywordsEng = model.KeywordsEnglish;

                    if (model.Image != null)
                    {
                        model.Image.SaveAs(Server.MapPath("~" + SiteSettings.NewsImageFolder) + model.Image.FileName);
                        news.ImageUrl = SiteSettings.NewsImageFolder + model.Image.FileName;
                    }

                    news.ModifiedDate = DateTime.Now;
                    news.ModifiedBy = HttpContext.User.Identity.Name;

                    news.Update();

                    return RedirectToAction(MVC.Admin.News.Index());
                }
            }
            return View(model);
        }

        public virtual ActionResult Delete(Guid Id)
        {
            News news = News.GetById(Id);

            if (news != null)
            {
                news.IsDeleted = true;
                news.Update();
            }

            return RedirectToAction(MVC.Admin.News.Index());
        }
    }
}
